var app = getApp()
let http = require('../../utils/http.js');
var page = 1
var loading = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: 1,  //标记热门搜索或历史点播 热门搜索为1, 历史点播为0
    hotSongs: [],
    historySongs: [],
    windowHeight: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: `点歌`
    })
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#FFFFFF',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight - res.windowWidth / 750 * 175
        });
      }
    });

    http.getSongList(page, (result, data) => {
      let originSongs = data.body.song_list;
      this.addNewSongsToHotSongs(this, originSongs)
    });

    /**
     * 历史纪录更新
     */
    this.setData({
      historySongs: app.globalData.historySongs,
    })
  },

  addNewSongsToHotSongs: function (manager, originSongs) {
    let hotSongs = this.data.hotSongs
    for (var i = 0; i < originSongs.length; i++) {
      let bufferred = false
      let bufferredFilePath = null
      if (originSongs[i].id in app.globalData.bufferredSongs) {
        bufferred = true;
        bufferredFilePath = app.globalData.bufferredSongs[originSongs[i].id]["bufferredFilePath"];
      }
      hotSongs.push({
        lrc: originSongs[i].lrc,
        name: originSongs[i].name,
        author: originSongs[i].author,
        link: originSongs[i].link,
        duration: originSongs[i].duration,
        id: originSongs[i].id,
        bufferred: bufferred,
        bufferredFilePath: bufferredFilePath,
      });
    }
    manager.setData({
      hotSongs: hotSongs,
    })
  },

  /**
   * 搜索界面跳转
   */
  searchController: function () {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  /**
   * 热门歌曲 历史记录界面切换
   */
  showController: function () {
    this.setData({
      show: !this.data.show
    })
  },

  /**
   * 缓冲歌曲 排麦
   */
  bufferSong(e) {
    let hotSongs = this.data.hotSongs;
    var songid = parseInt(e.currentTarget.id);        //button传递歌曲id
    for (var i = 0; i < hotSongs.length; i++) {
      if (hotSongs[i]["id"] == songid) {
        if (hotSongs[i]["bufferred"]) {
          var newHistory = this.data.historySongs
          newHistory.push(hotSongs[i])
          this.setData({
            historySongs: newHistory
          })
          app.globalData.historySongs = this.data.historySongs;

          let pages = getCurrentPages();//当前页面
          let prevPage = pages[pages.length - 2];//上一页面
          prevPage.setData({//直接给上移页面赋值
            newSong: hotSongs[i],
          });
          wx.navigateBack({//返回
            delta: 1
          })
        } else {
          hotSongs[i]["bufferred"] = true;
          this.setData({ hotSongs: hotSongs })
          app.globalData.bufferredSongs[songid] = hotSongs[i];
        }
      }
    }
    console.log("bufferredSongs" + JSON.stringify(this.data.bufferredSongs))
  },

  //下拉加载更多数据
  loadingMore: function (e) {
    if (loading == false) {
      this.setData({hasMore:true})
      loading = true
      ++page
      console.log("page: " + page)
      http.getSongList(page, (result, data) => {
        this.addNewSongsToHotSongs(this, data.body.song_list)
        loading = false
      });
    }
  },
})